package com.iplant.mes.controller.exc;

import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.EXCService;
import com.iplant.mes.service.po.exc.EXCStatRate;
import com.iplant.mes.service.po.exc.EXCUnusualTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * 异常处理
 *
 * @author YouWang·Peng
 * 2023-9-26 16:01:07
 */
@Slf4j
@RestController
@RequestMapping("/api/EXCUnusualTask")
public class EXCUnusualTaskController extends BasicController<EXCUnusualTask> {


    @Autowired
    EXCService mEXCService;

    public EXCUnusualTaskController() {
        super(EXCUnusualTask.class);
    }

    /**
     * 查询任务（网页版）
     */
    @GetMapping("/EmployeeAllWeb")
    public Object EmployeeAllWeb(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);

            if (!wParams.containsKey("Status")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<EXCUnusualTask>> wServiceResult =
                    mEXCService.BPM_QueryEmployeeAllWeb(wLoginUser, wParams, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @GetMapping("/TypeTrends")
    public Object TypeTrends(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"), -1);
            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));
            int wStatType = StringUtils.parseInt(request.getParameter("wStatType"), -1);

            Calendar wStartDate = StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate = StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<EXCStatRate>> wServiceResult = mEXCService.EXC_GetTypeRateList(wLoginUser, wProductID,
                    wDeviceID, wStationID, wStatType, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("TypeTrends", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/StationTrends")
    public Object StationTrends(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"), -1);
            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));
            int wStatType = StringUtils.parseInt(request.getParameter("wStatType"), -1);

            Calendar wStartDate = StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate = StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<EXCStatRate>> wServiceResult = mEXCService.EXC_GetStationRateList(wLoginUser,
                    wProductID, wDeviceID, wStationID, wStatType, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("StationTrends", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }


    @GetMapping("/DeviceTrends")
    public Object DeviceTrends(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);


            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"), -1);
            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));
            int wStatType = StringUtils.parseInt(request.getParameter("wStatType"), -1);

            Calendar wStartDate = StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate = StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<EXCStatRate>> wServiceResult = mEXCService.EXC_GetDeviceRateList(wLoginUser,
                    wProductID, wDeviceID, wStationID, wStatType, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("DeviceTrends", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }


    @GetMapping("/TypeStat")
    public Object TypeStat(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);


            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"), -1);
            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));

            Calendar wStartDate = StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate = StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<EXCStatRate>> wServiceResult = mEXCService.EXC_GetTypeRateList(wLoginUser, wProductID,
                    wDeviceID, wStationID, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("TypeStat", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/StationStat")
    public Object StationStat(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"), -1);
            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));

            Calendar wStartDate = StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate = StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<EXCStatRate>> wServiceResult = mEXCService.EXC_GetStationRateList(wLoginUser,
                    wProductID, wDeviceID, wStationID, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("StationStat", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/DeviceStat")
    public Object DeviceStat(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"), -1);
            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));

            Calendar wStartDate = StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate = StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<EXCStatRate>> wServiceResult = mEXCService.EXC_GetDeviceRateList(wLoginUser,
                    wProductID, wDeviceID, wStationID, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("DeviceStat", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    /**
     * 自动发起报工异常
     */
    @GetMapping("/AutoSendReportExc")
    public Object AutoSendReportExc(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wResponsorID = StringUtils.parseInt(request.getParameter("ResponsorID"));
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));
            String wRemark = StringUtils.parseString(request.getParameter("Remark"));
            if (wResponsorID <= 0 || wStationID <= 0 || StringUtils.isEmpty(wRemark)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mEXCService.EXC_AutoSendReportExc(wLoginUser, wResponsorID,
                    wStationID, wRemark);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "自动发起报工异常成功!", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
